package com.orangelife.mobile.housepurchasing.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.net.NetworkUtil;
import com.curry.android.util.PhoneCallUtil;
import com.curry.orangelife.mobile.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.orangelife.mobile.housepurchasing.activity.HouseShowingActivity;
import com.orangelife.mobile.housepurchasing.biz.HouseListHttpBiz;
import com.orangelife.mobile.util.BaseViewHolder;
import com.thirdsdk.qiniu.QiniuHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by HeYuCan on 2016/5/19.
 */
public class HouseListRightAdapter<T> extends FatherAdapter<T> {

    private Context mContext;

    private List<T> mList = new ArrayList<>();
    private DisplayImageOptions options;

    public HouseListRightAdapter(Context context, List<T> mList) {
        this.mContext = context;
        this.mList = mList;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.shop_header)
                .showImageForEmptyUri(R.drawable.shop_header)
                .showImageOnFail(R.drawable.shop_header)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new RoundedBitmapDisplayer(10))
                .build();
    }

    @Override
    public BaseAdapter setList(List<T> list) {
        this.mList = list;
        return HouseListRightAdapter.this;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_house, null);
        }
        LinearLayout llItemHouse = BaseViewHolder.get(convertView, R.id.ll_item_house);
        TextView tvHouseName = BaseViewHolder.get(convertView, R.id.tvHouseName);
        TextView tvHousePrice = BaseViewHolder.get(convertView, R.id.tvPrice);
        TextView tvUnitPrice = BaseViewHolder.get(convertView, R.id.tvUnitPrice);
        TextView tvHouseSize = BaseViewHolder.get(convertView, R.id.tv_house_size);
        ImageButton ibCall = BaseViewHolder.get(convertView, R.id.ib_call);
        ImageView ivImage = BaseViewHolder.get(convertView, R.id.ivPhoto);
        TextView saletype = BaseViewHolder.get(convertView, R.id.tv_saletype);

        Map<String, Object> map = (Map<String, Object>) mList.get(position);
        String sale =String.valueOf(map.get("saleType"));
        //等接口
        tvHouseName.setText(String.valueOf(map.get("adrInfo")));
        if(HouseListHttpBiz.saleType(sale)==0){
            tvHousePrice.setText(String.valueOf(map.get("salePrice")) );
            tvUnitPrice.setText("万元");
        }else{
            tvHousePrice.setText(String.valueOf(map.get("rentalPrice")) );
            tvUnitPrice.setText("元/月");
        }

        tvHouseSize.setText(String.valueOf(map.get("area") + "㎡"));
        saletype.setText(HouseListHttpBiz.saleTypeCn(sale));
        String imageURL=String.valueOf(map.get("cover"));
        ImageLoader.getInstance().displayImage(QiniuHelper.getUrlWithQiniuListLogo(imageURL), ivImage, options);
        llItemHouse.setOnClickListener(new listener(map));
        ibCall.setOnClickListener(new listener(map));
        return convertView;

    }



    class listener implements View.OnClickListener {

        private Map<String, Object> clikmap = null;

        public listener(Map<String, Object> map) {

            this.clikmap = map;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.ll_item_house:
                    if (!NetworkUtil.isConnectedNet(mContext)) {
                        Toast.makeText(
                                mContext,
                                mContext.getResources().getString(
                                        R.string.net_disconnected),
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString("saleID",String.valueOf(clikmap.get("saleID")));
                    intent.setClass(mContext, HouseShowingActivity.class);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);

                    break;
                case R.id.ib_call:
                    String telephone=String.valueOf(clikmap.get("hotline"));
                    PhoneCallUtil.getInstances().callPhone(mContext, telephone, "呼叫橙子置业房源委托热线?");

            }
        }

    }
}

